<template>
  <div>
    <el-button type="primary" @click="checkPrinter">检查打印机</el-button>
    <el-button type="success" @click="printTest">打印测试页</el-button>
    
    <el-dialog title="打印机设置" :visible.sync="dialogVisible" width="20%">
      <el-form label-width="120px">
        <el-form-item label="选择打印机">
          <el-select v-model="selectedPrinter" placeholder="请选择打印机">
            <el-option
              v-for="printer in printerList"
              :key="printer"
              :label="printer"
              :value="printer">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmPrinter">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
	
export default {
  data() {
    return {
      LODOP: null, // Lodop对象
      dialogVisible: false,
      printerList: [],
      selectedPrinter: '',
      isLodopInstalled: false
    }
  },
  mounted() {
    this.initLodop();
  },
  methods: {
    // 初始化Lodop
    initLodop() {
      try {
        this.LODOP = getLodop();
        if (this.LODOP) {
          this.isLodopInstalled = true;
          console.log('Lodop初始化成功');
        } else {
          this.$message.error('未检测到Lodop打印控件，请安装后刷新页面');
        }
      } catch (e) {
        console.error('Lodop初始化失败:', e);
        this.$message.error('Lodop初始化失败');
      }
    },
    
    // 检查打印机
    checkPrinter() {
      if (!this.isLodopInstalled()) return;
      
      this.printerList = this.LODOP.GET_PRINTER_LIST();
      if (this.printerList && this.printerList.length > 0) {
        this.dialogVisible = true;
        this.selectedPrinter = this.printerList[0];
      } else {
        this.$message.warning('未检测到打印机，请连接打印机后重试');
      }
    },
    
    // 确认打印机选择
    confirmPrinter() {
      this.dialogVisible = false;
      this.$message.success(`已选择打印机: ${this.selectedPrinter}`);
    },
    
    // 检查Lodop是否安装
    isLodopInstalled2() {
      if (!this.LODOP) {
        this.$message.error('Lodop未初始化，请刷新页面重试');
        return false;
      }
      return true;
    },
    
    // 打印测试页
    printTest() {
      if (!this.isLodopInstalled()) return;
      
      try {
        this.LODOP.PRINT_INIT("测试打印");
        this.LODOP.SET_PRINTER_INDEX(this.selectedPrinter || '');
        
        // 添加打印内容
        this.LODOP.ADD_PRINT_TEXT(20, 20, 200, 30, "这是测试打印内容");
        this.LODOP.ADD_PRINT_LINE(50, 20, 220, 20, 0, 1);
        this.LODOP.ADD_PRINT_TEXT(70, 20, 200, 30, `打印时间: ${new Date().toLocaleString()}`);
        
        // 打印预览
        // this.LODOP.PREVIEW();
        
        // 直接打印
        this.LODOP.PRINT();
      } catch (e) {
        console.error('打印出错:', e);
        this.$message.error('打印出错: ' + e.message);
      }
    },
    
    // 打印表格数据
    printTable(data, columns) {
      if (!this.isLodopInstalled()) return;
      
      try {
        this.LODOP.PRINT_INIT("表格打印");
        this.LODOP.SET_PRINTER_INDEX(this.selectedPrinter || '');
        
        // 添加标题
        this.LODOP.ADD_PRINT_TEXT(20, 20, 500, 30, "数据表格");
        this.LODOP.SET_PRINT_STYLEA(0, "FontSize", 16);
        this.LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        
        // 创建表格
        let top = 60;
        const rowHeight = 25;
        const colWidth = 100;
        
        // 表头
        columns.forEach((col, index) => {
          this.LODOP.ADD_PRINT_TEXT(top, 20 + index * colWidth, colWidth, rowHeight, col.title);
          this.LODOP.SET_PRINT_STYLEA(0, "Alignment", 2); // 居中
          this.LODOP.SET_PRINT_STYLEA(0, "Bold", 1);
        });
        
        // 表格内容
        data.forEach((item, rowIndex) => {
          columns.forEach((col, colIndex) => {
            this.LODOP.ADD_PRINT_TEXT(
              top + (rowIndex + 1) * rowHeight, 
              20 + colIndex * colWidth, 
              colWidth, 
              rowHeight, 
              item[col.key] || ''
            );
            this.LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
          });
        });
        
        // 打印预览
        this.LODOP.PREVIEW();
      } catch (e) {
        console.error('表格打印出错:', e);
        this.$message.error('表格打印出错: ' + e.message);
      }
    }
  }
}
</script>